package com.jic.market.controller.manage;

import com.alibaba.fastjson.JSON;
import com.jic.common.base.vo.Page;
import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.RestResult;
import com.jic.market.api.manage.PatchGrouponThemeApi;
import com.jic.market.common.constant.PatchGrouponThemeConstant;
import com.jic.market.entity.PatchGrouponTheme;
import com.jic.market.enums.PromotionStatusEnum;
import com.jic.market.enums.PromotionTypeEnum;
import com.jic.market.request.PatchGrouponThemeCopyRequest;
import com.jic.market.request.PatchGrouponThemeRequest;
import com.jic.market.request.ToUpdateStatusRequest;
import com.jic.market.request.after.TellAddAllocationRequest;
import com.jic.market.request.manager.patchgroup.PatchGroupIdListQueryRequest;
import com.jic.market.response.PageResponse;
import com.jic.market.response.PatchGrouponThemeResponse;
import com.jic.market.response.PromotionMerchantResponse;
import com.jic.market.service.PatchGrouponThemeService;
import com.jic.market.service.allocation.AllocationLogicService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;



/**
 * 拼团活动控制器
 *
 * @author guohuayong
 * @date: 2020年01月10日
 */
@Api(tags = {"拼团活动控制器"})
@Slf4j
@RestController
@RequestMapping("/patch-groupon-theme")
public class PatchGrouponThemeController implements PatchGrouponThemeApi {

    @Autowired
    PatchGrouponThemeService patchGrouponThemeService;

    @Resource
    private AllocationLogicService allocationLogicService;


    @PostMapping("/query-page")
    @Override
    public RestResult<PageResult<PatchGrouponThemeResponse>> queryPage(PatchGrouponThemeRequest form) {
        log.info("分页查询拼团活动，入参{}", JSON.toJSONString(form));

        Page page = new Page();
        page.setLimit(form.getLimit());
        page.setStart(form.getStart());

        PatchGrouponTheme patchGrouponThemeParam = new PatchGrouponTheme();
//        patchGrouponThemeParam.setId(form.getId());
//        patchGrouponThemeParam.setStatus(form.getStatus());

        BeanUtils.copyProperties(form, patchGrouponThemeParam);

        PageResult<PatchGrouponTheme> result = patchGrouponThemeService.queryPage(patchGrouponThemeParam, page);

        List<PatchGrouponThemeResponse> couponDetailResponses = result.getRows().stream().map(temp -> {
            PatchGrouponThemeResponse patchGrouponThemeResponse = new PatchGrouponThemeResponse();
            patchGrouponThemeResponse.setId(temp.getId());
//            patchGrouponThemeResponse.setActivityTitle(temp.getActivityTitle());
            patchGrouponThemeResponse.setStatus(temp.getStatus());

            return patchGrouponThemeResponse;
        }).filter(item -> item != null).collect(Collectors.toList());

        PageResult<PatchGrouponThemeResponse> pageResult = new PageResult<>();
        pageResult.setRows(couponDetailResponses);
        pageResult.setTotal(result.getTotal());
        log.info("分页查询拼团活动，出参{}", JSON.toJSONString(pageResult));
        return RestResult.success(pageResult);
    }


    @ApiOperation(value = "删除活动")
    @PostMapping(value = "/deleteInfo")
    @Override
    public RestResult<?> deleteInfo(@RequestBody PatchGrouponThemeRequest bo) {
        patchGrouponThemeService.delete(bo.getId());
        return RestResult.success(null);
    }

    @ApiOperation(value = "关闭活动")
    @PostMapping(value = "/close")
    @Override
    public RestResult<Integer> close(@RequestBody PatchGrouponThemeRequest bo) {
        PatchGrouponThemeRequest request = new PatchGrouponThemeRequest();
        request.setId(bo.getId());
        request.setStatus(PromotionStatusEnum.CLOSED.getCode());
        Integer ret = patchGrouponThemeService.updateById(request);
        return RestResult.success(ret);
    }

    @ApiOperation(value = "更新活动")
    @PostMapping(value = "/update")
    @Override
    public RestResult<Integer> updateById(@RequestBody PatchGrouponThemeRequest bo) {
        Integer ret = patchGrouponThemeService.updateById(bo);

        // 审核通过 状态，则调用分摊接口
        if(PromotionStatusEnum.AUDIT_PASS.getCode().equals(bo.getStatus())){
            log.info("-----审核成功，调用分摊服务，记录分摊信息----");
            // 参数 promotionId 转换为 String
            String promotionId =String.valueOf(bo.getId());
            TellAddAllocationRequest addReq =new TellAddAllocationRequest();
            addReq.setPromType(PromotionTypeEnum.GROUPON.getCode());
            addReq.setPromotionId(promotionId);
            allocationLogicService.addAllocation(addReq);
        }
        return RestResult.success(ret);
    }

    @ApiOperation(value = "查询拼团活动详情")
    @PostMapping(value = "/detail")
    @Override
    public RestResult<PatchGrouponThemeResponse> detail(@RequestBody PatchGrouponThemeRequest bo) {
        log.info("分页查询拼团详情，入参{}", JSON.toJSONString(bo));
        PatchGrouponThemeResponse response = patchGrouponThemeService.getPatchGrouponThemeByIdAndAdminOperate(bo);
        log.info("分页查询拼团详情，出参{}", JSON.toJSONString(response));
        return RestResult.success(response);
    }




    @ApiOperation(value = "查询拼团活动列表")
    @Override
    @PostMapping("/query")
    public RestResult<PageResponse<PatchGrouponThemeResponse>> query(@RequestBody PatchGrouponThemeRequest request) {
        PageResponse<PatchGrouponThemeResponse> entityList = patchGrouponThemeService.queryPage2(request);
        return RestResult.success(entityList);
    }

    @Override
    public RestResult<PromotionMerchantResponse> detail(HttpServletRequest servletRequest, PatchGrouponThemeRequest request) {
        return null;
    }

    @Override
    public RestResult<Integer> deleteByPromotionId(PatchGrouponThemeRequest request) {
        return null;
    }

    @Override
    public RestResult<Integer> saveBatchByPromotionId(PatchGrouponThemeRequest p) {
        return null;
    }

    @Override
    public RestResult<Integer> deleteById(PatchGrouponThemeRequest request) {
        return null;
    }

    @ApiOperation(value = "保存")
    @Override
    @PostMapping(value = "/save")
    public RestResult<Long> save(@RequestBody PatchGrouponThemeRequest request) {
        Long id = patchGrouponThemeService.save(request);
        return RestResult.success(id);
    }

    @ApiOperation(value = "更新状态")
    @Override
    @PostMapping(value = "/doUpdateStatus")
    public RestResult<Long> doUpdateStatus(@RequestBody ToUpdateStatusRequest req) {
        RestResult<Long> result = patchGrouponThemeService.doUpdateStatus(req);
        return result;
    }

    @ApiOperation(value = "复制")
    @PostMapping("copy")
    @Override
    public RestResult<Long> copy(@RequestBody PatchGrouponThemeCopyRequest request) {
        return patchGrouponThemeService.copy(request);
    }

    @ApiOperation(value = "保存为模板")
    @PostMapping("saveTemplate")
    @Override
    public RestResult<Long> saveTemplate(@RequestBody PatchGrouponThemeCopyRequest request) {
        return patchGrouponThemeService.saveTemplate(request);
    }

    @ApiOperation(value = "根据多个拼团id查询列表")
    @PostMapping("/queryListByIds")
    @Override
    public RestResult<List<PatchGrouponThemeResponse>> queryListByIds(PatchGroupIdListQueryRequest request) {
        return RestResult.success(patchGrouponThemeService.queryListByIds(request));
    }

}
